package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * The type Dish controller.
 */
@Api(tags = "菜品管理")
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin/dish")
@RestController
public class DishController {

    private final DishService dishService;

    private final RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO the dish dto
     * @return the result
     */
    @ApiOperation("新增菜品")
    @PostMapping
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.categoryId")
    public Result<?> create(@RequestBody DishDTO dishDTO) {
        dishService.create(dishDTO);
        return Result.success();
    }

    /**
     * 分页条件查询
     *
     * @param dishPageQueryDTO the dish page query dto
     * @return the result
     */
    @ApiOperation("分页条件查询")
    @GetMapping("/page")
    public Result<PageResult> readPage(DishPageQueryDTO dishPageQueryDTO) {
        PageResult pageResult = dishService.readPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 根据id批量删除
     *
     * @param ids the ids
     * @return the result
     */
    @ApiOperation("根据id批量删除")
    @DeleteMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result<?> deleteByIds(@RequestParam List<Long> ids) {
        dishService.deleteByIds(ids);
        return Result.success();
    }


    /**
     * 根据id查询回显
     *
     * @param id the id
     * @return the result
     */
    @ApiOperation("根据id查询回显")
    @GetMapping("/{id}")
    public Result<DishVO> infoById(@PathVariable Long id) {
        DishVO dishVO = dishService.infoById(id);
        return Result.success(dishVO);
    }


    /**
     * 修改菜品
     *
     * @param dishDTO the dish dto
     * @return the result
     */
    @ApiOperation("修改菜品")
    @PutMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result<?> updateById(@RequestBody DishDTO dishDTO) {
        dishService.updateById(dishDTO);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId the category id
     * @return the result
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> readListByCategoryId(Long categoryId) {
        List<Dish> list = dishService.readListByCategoryId(categoryId);
        return Result.success(list);
    }

    /**
     * 更新状态
     *
     * @param status the status
     * @param id     the id
     * @return the result
     */
    @ApiOperation("更新状态")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result<?> status(@PathVariable Integer status, Long id) {
        dishService.status(status, id);
        return Result.success();
    }

    /**
     * 清除redis存缓
     */
//    private void cleanCache(String pattern) {
//        Set keys = redisTemplate.keys(pattern);
//        redisTemplate.delete(keys);
//    }

}
